package com.vcredit.bird.service.creditcard;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.bird.api.IBankCardService;
import com.vcredit.creditcat.enumtype.BankCardTypeEnum;
import com.vcredit.creditcat.exception.CustCardExcetpion;
import com.vcredit.creditcat.exception.UnionCardInfoException;
import com.vcredit.bird.vo.creditcard.CardInfo;
import com.vcredit.bird.vo.creditcard.UnionPayResult;
import com.vcredit.bird.vo.custcard.BankCardBin;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;

@Service
public class CreditCardProcessService {

	/**
	 * 验卡接口url
	 */
	@Value("${card.verify}")
	private String VERIFY_CARD_URL;

	@Value("${credit.card.url}")
	private String UNIONPAY_URL;

	@Autowired
	private IBankCardService bankCardService;

	public Boolean verifyCard(String cardNo, String name, String idNo, String mobile, String bankNo, String source)
			throws Exception {

		BankCardBin cardBin = bankCardService.queryCardBin(cardNo);
		if (cardBin.getIsCreditCard().indexOf(BankCardTypeEnum.CREDIT.getDesc()) <= -1) {
			throw new CustCardExcetpion("非贷记卡");
		}

		return bankCardService.verifyCard(cardNo, name, idNo, mobile, bankNo, source);

	}

	/**
	 * 银联查询信用卡记录
	 * 
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@Log
	public CardInfo getUnionCardInfo(String nationalId, String accountNo, String vbsBid, String businessType)
			throws Exception {
		StringBuilder param = new StringBuilder();
		param.append("{\"nationalId\":\"" + nationalId + "\",");
		param.append("\"accountNo\":\"" + accountNo + "\",");
		param.append("\"vbsBid\":\"" + vbsBid + "\",");
		param.append("\"businessType\":\"" + businessType + "\"}");
		UnionPayResult<CardInfo> result = JacksonUtil.Json2Obj(HttpRequest.doJson(UNIONPAY_URL, param.toString()),
				new TypeReference<UnionPayResult<CardInfo>>() {
				});
		if (result.getResult().equals("0")) {
			return result.getContent();
		}
		throw new UnionCardInfoException(result.getContent() + ":" + result.getMessage());
	}
}
